package com.devcon5.StudentPlanner;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

public class Preferences extends PreferenceActivity {
	
	//Preferences available: Hide completed assignments, mark completed on grade, place completed assignments last in 
	//adapter, and sort assignments by due date
	//works with preferences.xml in xml/ folder
	
	//Tracker strings and tags for tracking preferences and accessing database
	public static final String CLASS_TAG = Preferences.class.getSimpleName();
	public static final String TRACKER_PREFERENCE = "preference";
	public static final String TRACKER_HIDE_COMPLETED = "hide completed assignments";
	public static final String TRACKER_COMPLETED_LAST = "completed last";
	public static final String TRACKER_COMPLETE_GRADED = "completed graded";
	public static final String TRACKER_SORT_BY = "sort by";


	private CheckBoxPreference hideCompletedCheckBox, completedLastCheckBox;
	private ListPreference sortByPreference;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);

		completedLastCheckBox = (CheckBoxPreference) findPreference("completedlastpref");
		hideCompletedCheckBox = (CheckBoxPreference) findPreference("hidecompletepref");

		hideCompletedCheckBox.setOnPreferenceClickListener( new OnPreferenceClickListener() {
		//set this preference for use with assignments classes
			public boolean onPreferenceClick(Preference preference) {
				updateCheckBoxes();
				return false;
			}
		});

		sortByPreference = (ListPreference) findPreference("sortbypref");
		//lists the sort by preferences
		sortByPreference.setSummary( sortByPreference.getValue() );
		//displays summary beneath the preference name
		sortByPreference.setOnPreferenceChangeListener( new OnPreferenceChangeListener() {
		
		
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				//on change set to new
				sortByPreference.setSummary( newValue.toString() );

				return true;
			}
		});

		updateCheckBoxes();
	}

	public void updateCheckBoxes()	
	{
		if( hideCompletedCheckBox.isChecked() )
		{
			
			completedLastCheckBox.setEnabled( false ); //you can't hide completed and put them last at the list too
		}
		else
		{
			
			completedLastCheckBox.setEnabled( true );
		}
	}

	
	public void updateSortBy()	//refresh function
	{
		sortByPreference.setSummary( sortByPreference.getValue() );
	}
	
	
	@Override
	protected void onDestroy() {
		
		super.onDestroy();
	}

}
